// pages/appCarRegistration/appCarRegistration.js
var dateTimePicker = require('../../utils/dateTimePicker.js');
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
		array1: ['请选择', '中国', '巴西', '日本'],
		index1:0,
		array2: ['请选择', '中国', '巴西', '日本'],
    arr:null,
		index2:0,
    region: [],
    date1: '请选择',
    date2: '请选择',
		region2: [],
		array3: ['请选择费用类型','公费','自费'],
		index3:0,
		// 存储选择框产生费用类型的值
		csfylxSeltval:'',
		// 存储金额的值
		csfylxJe: '',
    checkRadio:null,

    date: '2018-10-01',
    time: '12:00',
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    dateTimeArray2: null,
    dateTime2: null,
    startYear: 2000,
    endYear: 2050,
    loginName: app.globalData.loginName,
    applyType: "CarRegistrationApproval",

    endTime: null,
    remark: null,
    startTime: null,
    publicOrPrivate: null,
    destination: [],
    department: null,
    startMileage:null,
    endMileage:null,
    totalMileage:"--",

  },

  bindBlur1: function(e){
    console.log('失去焦点事件1', e.detail.value)
    this.setData({
      startMileage: e.detail.value
    })
  },
  bindBlur2: function (e) {
    console.log('失去焦点事件2', e.detail.value)
    this.setData({
      endMileage: e.detail.value
    })
    this.getTotalMileage()
  },
  getTotalMileage: function(){
    //
    var startMileage =  this.data.startMileage;
    var endMileage =  this.data.endMileage;
    var totalMileage = Number(endMileage) - Number(startMileage);
   
    this.setData({
      totalMileage: totalMileage
    })
  },

	bindPickerChange1: function(e) {
	  this.setData({
	    index1: e.detail.value
	  })
	},
	bindPickerChange2: function(e) {
	  this.setData({
	    index2: e.detail.value,
      arr: this.data.array2[e.detail.value]
	  })

    //设置公私用，车牌号，

    this.setData({
      checkRadio: this.data.arr.publicOrPrivate,
    })

    for(var i =0;i<this.data.array1.length;i++){
      if (this.data.arr.carNumber == this.data.array1[i].id){
        this.setData({
          index1: i,
        })
      }
    }
    
	},
  // 添加地址
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var temp = this.data.region;
    temp.push(e.detail.value);
    this.setData({
      region: temp
    })
  },
  // 删除地址
  delAddz: function (e) {
    var temp = this.data.region;
    temp.splice(e.currentTarget.id, 1);
    this.setData({
      region: temp
    })
  },
  bindDateChange1: function (e) {
    this.setData({
      date1: e.detail.value
    })
  },
  bindDateChange2: function (e) {
    this.setData({
      date2: e.detail.value
    })
  },
	bindPickerChange3: function(e) {
	  this.setData({
	    index3: e.detail.value,
			csfylxSeltval: this.data.array3[e.detail.value].name
	  })
	},
	// 获取金额输入的值
	getJe:function(e){
		console.log(e.detail);
		this.setData({
		  csfylxJe: e.detail.value.replace(/\s+/g, '')
		})
	},
	// 添加产生费用类型
	addCsfylx:function(e) {
		var temp = this.data.region2;
		var je = this.data.csfylxJe;
		var csfylx = this.data.csfylxSeltval;
		if(csfylx=='' || csfylx=='请选择费用类型'){
			wx.showToast({
				title:'请选择费用类型!',
				icon:'none',
				duration:2000
			})
		}else if(je==''){
			wx.showToast({
				title:'金额输入空!',
				icon:'none',
				duration:2000
			})
		}else{
			temp.push(csfylx+je);
			this.setData({
			  region2: temp
			})
		}
	},
	// 删除产生费用类型
	delCsfylx:function(e) {
		var temp = this.data.region2;
		temp.splice(e.currentTarget.id, 1);
		this.setData({
		  region2: temp
		})
	},
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
  },	 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();

    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
      dateTimeArray2: obj1.dateTimeArray,
      dateTime2: obj1.dateTime,
      loginName:app.globalData.loginName
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getCarApplyList();
    this.getDictByCode("carNumber")
    this.getDictByCode2("carExpense")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //加载数据字典
  getDictByCode: function (codes) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/getDictByCode', //接口地址
      data: {
        code: codes,
      },
      success: function (res) {
        console.log("数据字典=", res)
        res.data.data.unshift({ name: "请选择" })
        that.setData({
          array1: res.data.data,
        })
      }
    })
  },
  //加载数据字典
  getDictByCode2: function (codes) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/getDictByCode', //接口地址
      data: {
        code: codes,
      },
      success: function (res) {
        console.log("数据字典=", res)
        res.data.data.unshift({ name: "请选择费用类型" })
        that.setData({
          array3: res.data.data,
        })
      }
    })
  },

  //加载可用车辆
  getCar: function (codes) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/administrative/apply/getDictByCodeUnDisabled', //接口地址
      data: {
        code: codes,
      },
      success: function (res) {
        console.log("数据字典=", res)
        var carArray = [];
        for (var i = 0; i < res.data.data.length; i++) {
          carArray.push(res.data.data[i].name)
        }

        that.setData({
          carArray: carArray,
        })
      }
    })
  },
  //加载用车申请列表
  getCarApplyList: function (codes) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/administrative/apply/getCarApplyList', //接口地址
      data: {
        loginName: that.data.loginName,

      },
      success: function (res) {
        res.data.data.unshift({ applyNumber: "请选择" })
        console.log("数据字典=", res)
        that.setData({
          array2: res.data.data,
        })
      }
    })
  },

  changeDateTime1(e) {
    this.setData({
      dateTime1: e.detail.value
    });
  },
  changeDateTimeColumn(e) {
    var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },

  changeDateTime2(e) {
    this.setData({
      dateTime2: e.detail.value

    });

  },

  changeDateTimeColumn2(e) {
    var arr = this.data.dateTime2, dateArr = this.data.dateTimeArray2;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray2: dateArr,
      dateTime2: arr
    });
  },

  formSubmit: function (e) {
    var that = this;
    // var destinations = that.data.destination.join(",");

    // console.log('目的地，携带数据为：', destinations);

    console.log('startMileage：', that.data.startMileage);
    console.log('endMileage：', that.data.endMileage); 
    console.log('region2:', that.data.region2); 

    wx.showToast({
      icon: 'loading',
      title: '提交中',
      mask: true
    })
    e.detail.value.expense = that.data.region2.toString()

    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    //  return;
    let { startTime, endTime, destination, publicOrPrivate, remark, carNumber, startMileage, endMileage, linkApplyId } = e.detail.value;
    if (!linkApplyId) {
      wx.showToast({
        title: '请选择用车申请编号！',
        icon: 'none',
        duration: 2000
      })
      //wx.hideToast();
      return;
    }
    if (!startTime || !endTime || !carNumber || !remark || !destination || !publicOrPrivate || !startMileage || !endMileage || !linkApplyId) {
      wx.showToast({
        title: '请填写完整数据！',
        icon: 'none',
        duration: 2000
      })
      //wx.hideToast();
      return;
    }

    wx.request({
      url: app.globalData.apiUrl + '/wx/administrative/apply/addDo', //接口地址
      data: e.detail.value,
      success: function (res) {
        console.log("新增用车登记信息=", res)
        if (res.data.code == 0) {
          that.setData({
            endTime: null,
            remark: null,
            startTime: null,
            publicOrPrivate: null,
            destination: null,
            department: null,
            startMileage: null,
            endMileage: null,
            totalMileage: "--",
            index1:0,
            index2:0,
            index3:0,
            checkRadio: null,

          })
          wx.showToast({
            title: '操作成功！',
            icon: 'success',
            duration: 2000
          })

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }

      }
    })
  },  
})